package com.xinqing.bigdata.flink.datastream.operators;

import com.xinqing.bigdata.flink.datastream.model.Worker;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;
import java.util.Properties;

/**
 * @Author:CHQ
 * @Date:2021/5/20 16:18
 * @Description
 */
public class Filter {
    public static void main(String args[]) throws Exception {

        //env,port
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(new Configuration());

        //checkpoint conf
        CheckpointConfig checkpointConfig = env.getCheckpointConfig();
        checkpointConfig.setMaxConcurrentCheckpoints(1);
        checkpointConfig.setCheckpointTimeout(60000);
        checkpointConfig.setCheckpointInterval(60000);
        checkpointConfig.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);

        //Kafka conf
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "10.201.7.175:9092");
        properties.setProperty("group.id", "chq9");
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        // properties.setProperty("auto.offset.reset", "earliest");
        properties.setProperty("auto.offset.reset", "latest");

        ArrayList<Worker> list = new ArrayList<>();
        list.add(new Worker("1", "chq", "23"));
        list.add(new Worker("2", "cl", "66"));
        list.add(new Worker("3", "zjl", "46"));

        //筛选出age>50的worker
        SingleOutputStreamOperator<Worker> workerSingleOutputStreamOperator = env.fromCollection(list)
                .filter((FilterFunction<Worker>) value -> Integer.parseInt(value.getAge()) > 50).setParallelism(4);

        workerSingleOutputStreamOperator.print();

        env.execute();

    }
}
